package org.lq.shopping.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.lq.shopping.dao.ShopShippingAddressDao;
import org.lq.shopping.dao.impl.ShopShippingAddressDaoImpl;
import org.lq.shopping.entity.ShopShippingAddress;
import org.lq.shopping.service.ShopShippingAddressService;
import org.lq.shopping.util.DruidUtil;
import org.w3c.dom.stylesheets.LinkStyle;

import com.google.gson.Gson;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/** 
* @author zyq1397860074@qq.com 
* @version 创建时间：2018年9月27日 下午5:45:35 
*
*/
public class ShopShippingAddressServiceImpl implements ShopShippingAddressService {
	Jedis jedis = new Jedis();
	Gson gson = new Gson();
	ShopShippingAddressDao ssad=new ShopShippingAddressDaoImpl();
	
	public static void main(String[] args) {
		ShopShippingAddressServiceImpl ser = new ShopShippingAddressServiceImpl();
		Set<String>list = ser.getShopShippingAddressByUserId(1, 5, 5);
		for (String string : list) {
			System.out.println(string);
		}
	}
	@Override
	public Set<String> getShopShippingAddressByUserId(int pageIndex,int pageSize,int id) {
		List<ShopShippingAddress> addressList = ssad.getShopShippingAddressByUserId(id);
		JedisPoolConfig config = new JedisPoolConfig();
		
		config.setMaxTotal(50);
		//最大空闲数
		config.setMaxIdle(5);
		//默认-1 等待时间没有获取到则抛出异常
		config.setMaxWaitMillis(1000*100);
		//获取连接前先验证
		config.setTestOnBorrow(true);
		//当返回一个连接给连接池先进行ping()判断是否可以连接
		config.setTestOnReturn(true);
		JedisPool pool = new  JedisPool(config,"127.0.0.1",6379,2000);
		jedis = pool.getResource();
//		jedis.auth("123456");
//		jedis.select(0);
//		jedis.zadd("stringlist",1, "AAAA"); 
//        jedis.zadd("stringlist",2, "BBBB"); 
//        jedis.zadd("stringlist",3, "CCCC");
//        jedis.zadd("stringlist",4, "DDDD");
//        jedis.zadd("stringlist",5, "EEEE");
//        jedis.zadd("stringlist",6, "FFFF");
//        jedis.zadd("stringlist",7, "GGGG");
//        jedis.zadd("stringlist",8, "HHHH");
		for (ShopShippingAddress shopShippingAddress : addressList) {
			String str = gson.toJson(shopShippingAddress);
			jedis.zadd("addressList",shopShippingAddress.getAddress_id(), str);
			
		}
		int max = pageIndex*pageSize-1;
		int min = (pageIndex-1)*pageSize;
		Set<String>list = jedis.zrange("stringlist", min, max);
		return list;
	}

	@Override
	public boolean addAddress(ShopShippingAddress address) {
		return ssad.add(address)>0;
	}

	@Override
	public boolean updateAddress(ShopShippingAddress address) {
		return ssad.update(address)>0;
	}

	@Override
	public boolean deleteAddress(int id) {
		return ssad.delete(id)>0;
	}

}
